package com.phone580.app;

import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import com.phone580.app.entity.DownloadInfoEntity;


public class Worker implements Runnable {
	private Object lock = new Object();
	private boolean done = false;
	private URL httpurl;
	private HttpURLConnection connection;
	private RandomAccessFile randomAccessFile;
	private InputStream in;
	private DownloadInfoEntity info;
	private CompleteCallback callback;
	
	public Worker(DownloadInfoEntity info, CompleteCallback callback) {
		try {
			System.out.println(info.getFileName() + "----standby");
			this.info = info;
			this.callback = callback;
			httpurl = new URL(info.getUrl());
			connection = (HttpURLConnection) httpurl.openConnection();
			connection.setConnectTimeout(5000);
			connection.setRequestMethod("GET");
			if (info.getFileSize() != 0) {
				connection.setRequestProperty(
						"Range",
						"bytes=" + info.getCompleteSize());
			}
			randomAccessFile = new RandomAccessFile(info.getFilePath(), "rwd");
			randomAccessFile.seek(info.getCompleteSize());
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public void run() {
		Thread download = new Thread(new Downloader());
		synchronized (lock) {
			download.start();
			try {
				lock.wait();
			} catch (InterruptedException e) {
				done = true;
				try {
					connection.disconnect();
					randomAccessFile.close();
					if (in != null) {
						in.close();
					}
					System.out.println(info.getFileName() + "----block");
				} catch (IOException ioe) {
					ioe.printStackTrace();
				} 
			} finally {
				try {
					connection.disconnect();
					randomAccessFile.close();
					if (in != null) {
						in.close();
					}
				} catch (IOException ioe) {
					ioe.printStackTrace();
				} 
			}
		}
		
	}
	
	class Downloader implements Runnable{

		@Override
		public void run() {
			try {
				if (done) {
					return;
				}
				info.setStatus(Constant.DOWNLOAD_START);
				if (info.getFileSize() == 0) {
					int fileSize = connection.getContentLength();
					info.setFileSize(fileSize);
				}
				in = connection.getInputStream();
				byte[] buffer = new byte[4096];
				int length = -1;
				System.out.println(info.getFileName()+ "----downloading");
				int completeSize = info.getCompleteSize();
				while ((length = in.read(buffer)) != -1 && !done) {
					if (length == 0) {
						continue;
					}
					randomAccessFile.write(buffer, 0, length);
					completeSize += length;
					info.setCompleteSize(completeSize);
//					System.out.println("file_size = " + info.getFileSize() + "--------------------" + "complete_size = " + info.getCompleteSize());
				}
				System.out.println(info.getFileName() + "----done");
				info.setStatus(Constant.DOWNLOAD_FINISH);
				callback.complete(info);
			} catch (IOException e) {
				info.setStatus(Constant.DOWNLOAD_PAUSE);
				callback.stop(info);
//				e.printStackTrace();
			} finally {
				synchronized (lock) {
					done = true;
					lock.notify();
				}
			}
		}
		
	}

	public interface CompleteCallback {
		public void complete(DownloadInfoEntity info);
		
		public void stop(DownloadInfoEntity info);
	}
}
